﻿namespace EasyGo.Common.Auth
{
    using System;
    using System.Security.Principal;

    public class EasyGoPrincipal : GenericPrincipal
    {
        private static Func<EasyGoPrincipal> principalResolver;

        public EasyGoPrincipal(string email, string name, int userId, UserRole role, IIdentity identity)
            : base(identity, new[] { role.Key })
        {
            this.Name = name;
            this.Role = role;
            this.Id = userId;
        }

        public static new EasyGoPrincipal Current
        {
            get
            {
                if (principalResolver == null)
                {
                    throw new Exception("No principal resolver has been configured");
                }

                return principalResolver();
            }
        }

        public int Id { get; private set; }

        public string Name { get; private set; }

        public UserRole Role { get; private set; }

        public static void SetResolver(Func<EasyGoPrincipal> resolver)
        {
            principalResolver = resolver;
        }
    }
}
